local skel = fk.CreateSkill {
  name = "rmt__shengshengbuxi",
  tags = {Skill.Compulsory},
}

Fk:loadTranslationTable{
  ["rmt__shengshengbuxi"] = "生生不息",
  [":rmt__shengshengbuxi"] = "锁定技，你最后一张手牌具有“绥”标记。",

  ["@@rmt_appease"] = "绥",
  ["rmt__huaiyuan_active"] = "怀远",
  ["huaiyuan"] = "怀远",
  ["#rmt__huaiyuan-invoke"] = "怀远：令一名角色手牌上限+1 / 攻击范围+1 / 摸一张牌",
  ["rmt__huaiyuan_maxcards"] = "手牌上限+1",
  ["rmt__huaiyuan_attackrange"] = "攻击范围+1",
  ["@rmt__huaiyuan_maxcards"] = "怀远:上限+",
  ["@rmt__huaiyuan_attackrange"] = "怀远:范围+",
}

skel:addEffect(fk.AfterCardsMove, {
  anim_type = "support",
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(skel.name) and player:getHandcardNum() == 1 then
      for _, move in ipairs(data) do
        if move.to == player and move.toArea == Player.Hand then return true end
        if move.from == player and table.find(move.moveInfo, function(info) return info.fromArea == Player.Hand end) then
          return true
        end
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    player.room:setCardMark(Fk:getCardById(player.player_cards[Player.Hand][1]), "@@rmt_appease", 1)
  end,
})

skel:addEffect(fk.AfterCardsMove, {
  can_refresh = Util.TrueFunc,
  on_refresh = function(self, event, target, player, data)
    for _, move in ipairs(data) do
      if move.from == player then
        for _, info in ipairs(move.moveInfo) do
          if Fk:getCardById(info.cardId):getMark("@@rmt_appease") ~= 0 then
            player.room:setCardMark(Fk:getCardById(info.cardId), "@@rmt_appease", 0)
            move.extra_data = move.extra_data or {}
            move.extra_data.shengshengbuxiCheck = true
          end
        end
      end
      if move.to == player and player:getHandcardNum() > 1 then
        for _, id in ipairs(player:getCardIds("h")) do
          if Fk:getCardById(id):getMark("@@rmt_appease") ~= 0 then
            player.room:setCardMark(Fk:getCardById(id), "@@rmt_appease", 0)
          end
        end
      end
    end
  end,
})

skel:addEffect(fk.AfterCardsMove, {
  mute = true,
  priority = 0.99,
  can_trigger = function(self, event, target, player, data)
    if not player.dead then
      for _, move in ipairs(data) do
        if move.from == player and #move.moveInfo > 0
        and move.extra_data and move.extra_data.shengshengbuxiCheck then
          return true
        end
      end
    end
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local _, dat = room:askToUseActiveSkill(player, {
      skill_name = "rmt__huaiyuan_active", prompt = "#rmt__huaiyuan-invoke", cancelable = false
    })
    local to = dat and dat.targets[1] or player
    local choice = dat and dat.interaction or "draw1"
    room:notifySkillInvoked(player, "huaiyuan", "support", {to})
    if choice == "draw1" then
      to:drawCards(1, "huaiyuan")
    else
      room:addPlayerMark(to, "@"..choice, 1)
    end
  end,
})

skel:addEffect("atkrange", {
  correct_func = function (self, from, to)
    return from:getMark("@rmt__huaiyuan_attackrange")
  end,
})

skel:addEffect("maxcards", {
  correct_func = function(self, player)
    return player:getMark("@rmt__huaiyuan_maxcards")
  end,
})

skel:addLoseEffect(function (self, player, is_death)
  for _, id in ipairs(player:getCardIds("h")) do
    if Fk:getCardById(id):getMark("@@rmt_appease") ~= 0 then
      player.room:setCardMark(Fk:getCardById(id), "@@rmt_appease", 0)
    end
  end
end)

return skel
